<template>
  <div class="PeopleAdmin">
    <a-card>
      <a-form :form="form">
        <a-row :gutter="30">
          <a-col :span="8">
            <a-form-item label="手机号" :label-col="{ span: 6 }" :wrapper-col="{ span: 16,offset: 1 }">
              <a-input
                placeholder="请输入手机号"
                v-decorator="[
          'phone',
          {rules: [{ required: true, message: '请输入手机号!' }]}
        ]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="名称" :label-col="{ span: 6 }" :wrapper-col="{ span: 16,offset: 1 }">
              <a-input
                placeholder="请输入姓名/昵称"
                v-decorator="[
          'name',
          {rules: [{ required: true, message: '请输入名称!' }]}
        ]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="角色" :label-col="{ span: 6 }" :wrapper-col="{ span: 16,offset: 1 }">
              <a-select
                style="width:100%"
                v-decorator="[
          'role',
          {rules: [{ required: true, message: '请选择角色!' }]}
        ]"
                placeholder="全部"
              >
                <a-select-option value="all">全部</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="30">
          <a-col :span="8">
            <a-form-item label="状态" :label-col="{ span: 6 }" :wrapper-col="{ span: 16,offset: 1 }">
              <a-select
                style="width:100%"
                v-decorator="[
          'status',
          {rules: [{ required: true, message: '请选择状态!' }]}
        ]"
                placeholder="全部"
              >
                <a-select-option value="0">全部</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="15" :style="{ textAlign: 'right' }">
            <a-button type="primary" @click="search">查询</a-button>
            <a-button style="margin-left:2%">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </a-card>
    <a-card style="margin-top:2%">
      <a-button type="primary" icon="plus" @click="showModal()">新建</a-button>
      <a-table
        style="margin-top:2%"
        :columns="columns"
        :dataSource="data"
        :pagination="pagination"
        @change="handleTableChange"
        :rowKey="record => record.managerId"
      >
        <template slot="status" slot-scope="text,record">
          <a-switch checkedChildren="开" unCheckedChildren="关" :defaultChecked="record.status"/>
        </template>
        <template slot="operate" slot-scope="text,record">
          <span class="blue" @click="showEdit(text,record)">编辑</span>
          <span class="red" @click="showDeleteConfirm(text,record)">删除</span>
        </template>
      </a-table>
    </a-card>
    <a-modal
      title="编辑人员信息"
      :visible="visibleEdit"
      @ok="handleEditOk"
      @cancel="handleEditCancel"
      v-model="visibleEdit"
    >
      <template slot="footer">
        <a-button key="back" @click="handleEditCancel">取消</a-button>
        <a-button key="submit" type="primary" @click="handleEditOk">确定</a-button>
      </template>
      <a-form :form="form">
        <a-form-item label="姓名" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input
            style="width: 100%"
            placeholder="请输入"
            v-decorator="[
          'nameEdit',
          {rules: [{ required: true, message: '请输入姓名!' }]} 
        ]"
          />
        </a-form-item>
        <a-form-item label="用户名" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input
            style="width: 100%"
            placeholder="请输入"
            v-decorator="[
          'userNameEdit',
          {rules: [{ required: true, message: '请输入用户名!' }]} 
        ]"
          />
        </a-form-item>
        <a-form-item label="密码" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input
            type="password"
            style="width: 100%"
            placeholder="请输入"
            v-decorator="[
          'passwordEdit',
          {rules: [{ required: true, message: '请输入密码!' }]} 
        ]"
          />
        </a-form-item>
        <a-form-item label="手机号" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input
            style="width: 100%"
            placeholder="请输入"
            v-decorator="[
          'phoneEdit',
          {rules: [{ required: true, message: '请输入手机号!' }]} 
        ]"
          />
        </a-form-item>
        <a-form-item label="角色" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-select
            style="width: 100%"
            v-decorator="[
          'roleEdit',
          {rules: [{ required: true, message: '请选择角色!' }]}
        ]"
            placeholder="超级管理员"
          >
            <a-select-option value="0">角色A</a-select-option>
            <a-select-option value="1">角色B</a-select-option>
            <a-select-option value="2">角色C</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="备注" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input
            style="width: 100%"
            placeholder="请输入"
            v-decorator="[
          'noteEdit',
          {rules: [{ required: true, message: '请输入备注!' }]} 
        ]"
          />
        </a-form-item>
        <a-form-item label="启用状态" :label-col="{ span: 5 }">
          <a-switch
            v-decorator="['statusEdit', { valuePropName: 'checked',initialValue:true }]"
            checkedChildren="开"
            unCheckedChildren="关"
          />
        </a-form-item>
      </a-form>
    </a-modal>
    <a-modal
      title="新增人员"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      v-model="visible"
    >
      <template slot="footer">
        <a-button key="back" @click="handleCancel">取消</a-button>
        <a-button key="submit" type="primary" @click="handleOk">确定</a-button>
      </template>
      <a-form :form="form">
        <a-form-item label="姓名" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input
            placeholder="请输入"
            v-decorator="[
          'nameAdd',
          {rules: [{ required: true, message: '请输入姓名!' }]} 
        ]"
          />
        </a-form-item>
        <a-form-item label="用户名" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input
            placeholder="请输入"
            v-decorator="[
          'userNameAdd',
          {rules: [{ required: true, message: '请输入用户名!' }]}
        ]"
          />
        </a-form-item>
        <a-form-item label="密码" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input
            type="password"
            placeholder="请输入"
            v-decorator="[
          'passwordAdd',
          {rules: [{ required: true, message: '请输入用密码!' }]}
        ]"
          />
        </a-form-item>
        <a-form-item label="手机号" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input
            placeholder="请输入"
            v-decorator="[
          'phoneAdd',
          {rules: [{ required: true, message: '请输入手机号!' }]}
        ]"
          />
        </a-form-item>
        <a-form-item label="角色" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-select
            style="width: 100%"
            v-decorator="[
          'roleAdd',
          {rules: [{ required: true, message: '请选择角色!' }]}
        ]"
            placeholder="请选择"
          >
            <a-select-option value="0">角色A</a-select-option>
            <a-select-option value="1">角色B</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="备注" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input
            placeholder="请输入"
            v-decorator="[
          'noteAdd',
          {rules: [{ required: true, message: '请输入备注!' }]}
        ]"
          />
        </a-form-item>
        <a-form-item label="启用状态" :label-col="{ span: 5 }">
          <a-switch
            v-decorator="['statusAdd',{ valuePropName: 'checked',initialValue:true }]"
            checkedChildren="开"
            unCheckedChildren="关"
          />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
const columns = [
  {
    title: "用户名",
    dataIndex: "username",
    scopedSlots: { customRender: "userName" },
    align: "center"
  },
  {
    title: "姓名",
    dataIndex: "nickname",
    scopedSlots: { customRender: "userName" },
    align: "center"
  },
  {
    title: "手机号",
    dataIndex: "cellphone",
    scopedSlots: { customRender: "phone" },
    align: "center"
  },
  {
    title: "用户角色",
    dataIndex: "authorityName",
    scopedSlots: { customRender: "userName" },
    align: "center"
  },
  {
    title: "状态",
    dataIndex: "status",
    scopedSlots: { customRender: "status" },
    align: "center"
  },
  {
    title: "最近登陆时间",
    dataIndex: "loginTime",
    scopedSlots: { customRender: "latelyTime" },
    align: "center"
  },
  {
    title: "操作",
    dataIndex: "",
    scopedSlots: { customRender: "operate" },
    align: "center"
  }
];
const data = [
  {
    key: "1",
    userName: "1234",
    name: "口水娃玉米味烤肠袋装50g",
    phone: "",
    userRole: "1",
    latelyTime: ""
  }
];
export default {
  name: "PeopleAdmin",
  data() {
    return {
      form: this.$form.createForm(this),
      columns,
      data: [],
      pagination: {
        showSizeChanger: true,
        showQuickJumper: true,
        pageSizeOptions: ["2", "4", "8", "10", "12"],
        showTotal: (total, range) => {
          return `${range[0]}-${range[1]} 共${total}条`;
        },
        pageSize: 8, //每页条数
        defaultCurrent: 1, //默认当前页
        current: 1, //当前页
        total: 500 //数据总条数
      },
      visible: false,
      visibleEdit: false
    };
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination };
      pager.current = pagination.current;
      pager.pageSize = pagination.pageSize;
      pager.total = pagination.total;
      this.pagination = pager;
      this.search();
    },
    showModal() {
      this.visible = true;
    },
    handleOk(e) {
      this.visible = false;
      this.addManager();
    },
    handleCancel(e) {
      this.visible = false;
    },
    showEdit(text, record) {
      this.visibleEdit = true;
      this.managerId = record.managerId;
    },
    handleEditOk(e) {
      this.visibleEdit = false;
      this.editManager();
    },
    handleEditCancel(e) {
      this.visibleEdit = false;
    },
    showDeleteConfirm(text, record) {
      var that = this;
      this.managerIds = record.managerId;
      this.$confirm({
        title: "确认要删除此人员吗？",
        content: "删除此人员后，此人员无法登录",
        okText: "确定",
        cancelText: "取消",
        iconType: "close-circle",
        onOk() {
          that.deleteManager(that.managerIds);
        },
        onCancel() {}
      });
    },

    // 返回管理人员列表数据
    search() {
      var values = this.form.getFieldsValue();
      this.axios
        .post(this.baseUrl + "/manager/search", {
          loginToken: this.$store.state.loginToken,
          pageNum: this.pagination.current,
          pageSize: this.pagination.pageSize,
          authorityId: values.role,
          cellphone: values.phone,
          nickname: values.name,
          sortType: 1,
          status: values.status
        })
        .then(response => {
          // console.log(response);
          this.data = response.data.data.list;
          this.pagination.total = response.data.data.total;
          this.pagination.pageSize = response.data.data.pageSize;
          this.pagination.current = response.data.data.pageNum;
        });
    },

    // 新增管理人员
    addManager() {
      var values = this.form.getFieldsValue();
      this.axios
        .post(this.baseUrl + "/manager/add", {
          loginToken: this.$store.state.loginToken,
          authorityId: values.roleAdd,
          cellphone: values.phoneAdd,
          nickname: values.nameAdd,
          password: values.passwordAdd,
          remark: values.noteAdd,
          status: values.statusAdd,
          username: values.userNameAdd
        })
        .then(response => {
          if (response.data.code == 0) {
            this.$message.info("新增成功");
            this.search();
          }
          if (response.data.code == -1) {
            this.$message.info(response.data.msg);
          }
        });
    },

    // 删除管理人员
    deleteManager(managerIds) {
      var values = this.form.getFieldsValue();
      this.axios
        .post(this.baseUrl + "/manager/delete", {
          loginToken: this.$store.state.loginToken,
          managerIds: [managerIds]
        })
        .then(response => {
          console.log(response);
          if (response.data.code == 0) {
            this.$message.info("删除成功");
            this.search();
          }
          if (response.data.code == -1) {
            this.$message.info(response.data.msg);
          }
        });
    },

    // 编辑管理人员，不需要更新的字段用null
    editManager() {
      var values = this.form.getFieldsValue();
      this.axios
        .post(this.baseUrl + "/manager/edit", {
          loginToken: this.$store.state.loginToken,
          managerId: this.managerId,
          authorityId: values.roleEdit,
          cellphone: values.phoneEdit,
          nickname: values.nameEdit,
          password: values.passwordEdit,
          remark: values.noteEdit,
          status: values.statusEdit,
          username: values.userNameEdit
        })
        .then(response => {
          console.log(response);
          if (response.data.code == 0) {
            this.$message.info("编辑成功");
            this.search();
          }
          if (response.data.code == -1) {
            this.$message.info(response.data.msg);
          }
        });
    }
  },
  created() {
    this.search();
  },
  mounted() {}
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.PeopleAdmin {
  width: 100%;
}

.edit {
  color: rgb(0, 85, 255);
}
</style>
